@echo off

setlocal EnableDelayedExpansion
pushd .

if "%~1"=="" goto end
if "%~x1"==".tmp" (
	for /F "delims=*" %%i in (%~1) do call :lsfn_sub "%%~i"
) else (
	call :lsfn_sub "%~1"
)
goto end

REM ========================================================

:lsfn_sub
set errno=0
if "%~1"=="" (
  echo Using current working directory: !CD!
) else (
    cd /d "%~1"
    if errorlevel 1 echo ERROR: %~1: cannot chdir&&pause&&exit /b -1
)

dir /b >\___lsfn_episodes.txt
if errorlevel 1 echo WARNING: cannot save "_episodes.txt"&& set /a errno+=1
for /f "delims=*" %%i in ("!CD!") do echo %%~nxi >\___lsfn_title.txt
if errorlevel 1 echo WARNING: cannot save "_title.txt"&& set /a errno+=1

move /Y \___lsfn_episodes.txt .\_episodes.txt
if errorlevel 1 echo WARNING: cannot move "_episodes.txt"&& set /a errno+=1
move /Y \___lsfn_title.txt .\_title.txt
if errorlevel 1 echo WARNING: cannot move "_title.txt"&& set /a errno+=1
:diserror
if !errno!==0 (
    echo DoNE
) else (
    echo ERROR: code: !errno!
    pause
)
goto :eof

:end
popd
endlocal
